<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="beans.UseOrder" %>
<%@ page import="beans.Shenqing" %>
<%@ page import="beans.Employee" %>
<%@ page import="service.VehicleService" %>
<%@ page import="service.DriversService" %>
<%@ page import="service.ShenqingService" %>
<%@ page import="beans.Vehicle" %>
<%@ page import="beans.Drivers" %>
<%
    // 从会话中获取用户信息
    Employee employee = (Employee) session.getAttribute("user");
    String role = (String) session.getAttribute("role");
    String username = (String) session.getAttribute("username");

    // 如果没有登录，重定向到登录页面
    if (employee == null || username == null) {
        response.sendRedirect("login.jsp");
        return;
    }

    // 获取订单信息（从servlet传递过来的）
    UseOrder order = (UseOrder) request.getAttribute("order");
    Shenqing shenqing = (Shenqing) request.getAttribute("shenqing");
    String orderStatus = (String) request.getAttribute("orderStatus");
    String searchOrderId = (String) request.getAttribute("searchOrderId");
    String errorMessage = (String) request.getAttribute("errorMessage");

    // 如果没有从servlet获取到任何信息，重定向到servlet进行查询
    if (orderStatus == null) {
        response.sendRedirect(request.getContextPath() + "/UseOrderServlet?action=progress");
        return;
    }

    // 初始化服务类，用于获取车辆和司机信息
    VehicleService vehicleService = new VehicleService();
    DriversService driversService = new DriversService();
    ShenqingService shenqingService = new ShenqingService();

    // 获取最新申请订单号
    Shenqing latestShenqing = shenqingService.getLatestShenqingByEmployeeId(employee.getEmployeeID());
    String latestOrderId = latestShenqing != null ? String.valueOf(latestShenqing.getOrderId()) : null;
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>公司用车管理系统（用车员工）</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
        }

        .header {
            background: #183a8a;
            color: #fff;
            font-size: 2rem;
            font-weight: bold;
            padding: 10px 0 10px 10px;
            letter-spacing: 2px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .header-right {
            display: flex;
            align-items: center;
            margin-right: 30px;
            font-size: 1rem;
        }

        .user-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            color: #183a8a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin-right: 10px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .user-info {
            margin-right: 20px;
            color: #fff;
        }

        .bell-icon {
            font-size: 1.3rem;
            margin-right: 20px;
            color: #fff;
            cursor: pointer;
            transition: transform 0.2s;
        }

        .bell-icon:hover {
            transform: scale(1.1);
        }

        .logout {
            color: #fff;
            text-decoration: none;
            margin-left: 10px;
            font-size: 1rem;
            transition: color 0.2s;
        }

        .logout:hover {
            color: #a8c5ff;
        }

        .container {
            display: flex;
            height: calc(100vh - 56px);
        }

        .sidebar {
            width: 220px;
            background: #183a8a;
            display: flex;
            flex-direction: column;
            padding-top: 10px;
            box-shadow: 2px 0 8px rgba(24, 58, 138, 0.05);
        }

        .menu-item {
            color: #fff;
            padding: 18px 0 18px 32px;
            font-size: 1.15rem;
            cursor: pointer;
            border-left: 5px solid transparent;
            transition: background 0.2s, border 0.2s, transform 0.2s;
            text-decoration: none;
            display: block;
        }

        .menu-item.active,
        .menu-item:hover {
            background: #2450a7;
            border-left: 5px solid #fff;
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(24, 58, 138, 0.10);
            transform: translateX(2px);
        }

        .main-content {
            flex: 1;
            background: #f6f6f6;
            padding: 30px;
            overflow-y: auto;
        }

        .form-card {
            background: #ffffff;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            padding: 25px;
            max-width: 900px;
            margin: 0 auto;
        }

        .form-title {
            font-size: 1.7rem;
            font-weight: bold;
            color: #183a8a;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        /* 最新订单号显示样式 */
        .latest-order-info {
            background: linear-gradient(135deg, #e8f5e9 0%, #f3e5f5 100%);
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 25px;
            border-left: 6px solid #4caf50;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
        }

        .latest-order-info h4 {
            margin: 0 0 10px 0;
            font-size: 1.2rem;
            color: #2e7d32;
            display: flex;
            align-items: center;
        }

        .latest-order-info h4 .icon {
            font-size: 1.4rem;
            margin-right: 10px;
        }

        .latest-order-id {
            font-size: 1.3rem;
            font-weight: bold;
            color: #1565c0;
            background: #e3f2fd;
            padding: 8px 15px;
            border-radius: 6px;
            display: inline-block;
            margin-top: 5px;
        }

        /* 搜索框样式 */
        .search-section {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 25px;
            border: 1px solid #e9ecef;
        }

        .search-form {
            display: flex;
            gap: 15px;
            align-items: end;
        }

        .search-group {
            flex: 1;
        }

        .search-group label {
            display: block;
            font-weight: 500;
            margin-bottom: 5px;
            color: #333;
        }

        .search-group input {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 1rem;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .search-group input:focus {
            outline: none;
            border-color: #2450a7;
            box-shadow: 0 0 0 3px rgba(36, 80, 167, 0.15);
        }

        .search-btn {
            background: #2450a7;
            color: #fff;
            border: none;
            border-radius: 6px;
            padding: 10px 20px;
            cursor: pointer;
            font-size: 1rem;
            transition: background 0.2s, transform 0.2s;
            white-space: nowrap;
        }

        .search-btn:hover {
            background: #1a4080;
            transform: translateY(-1px);
        }

        .clear-btn {
            background: #6c757d;
            color: #fff;
            border: none;
            border-radius: 6px;
            padding: 10px 15px;
            cursor: pointer;
            font-size: 1rem;
            transition: background 0.2s;
            white-space: nowrap;
        }

        .clear-btn:hover {
            background: #5a6268;
        }

        /* 错误消息样式 */
        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
            display: flex;
            align-items: center;
        }

        .error-icon {
            margin-right: 10px;
            font-size: 1.2rem;
        }

        /* 订单信息样式 */
        .order-info {
            background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
            border-radius: 12px;
            padding: 30px;
            margin-bottom: 20px;
            border-left: 6px solid #1565c0;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
        }

        .order-info h3 {
            margin: 0 0 20px 0;
            font-size: 1.4rem;
            color: #1565c0;
            display: flex;
            align-items: center;
        }

        .order-info h3 .icon {
            font-size: 1.6rem;
            margin-right: 10px;
        }

        .order-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .detail-item {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
            border-left: 4px solid #2450a7;
        }

        .detail-item label {
            display: block;
            font-weight: 600;
            color: #333;
            margin-bottom: 8px;
            font-size: 0.95rem;
        }

        .detail-item .value {
            font-size: 1.1rem;
            color: #1565c0;
            font-weight: 500;
            word-wrap: break-word;
        }

        .status-badge {
            display: inline-block;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 1rem;
            font-weight: 600;
            text-align: center;
            margin-top: 5px;
        }

        .status-pending {
            background-color: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
        }

        .status-approved {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status-rejected {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .status-processing {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }

        .status-completed {
            background-color: #e6f7ff;
            color: #1890ff;
            border: 1px solid #91d5ff;
        }

        .status-default {
            background-color: #f5f5f5;
            color: #666;
            border: 1px solid #d9d9d9;
        }

        .status-reviewing {
            background-color: #e6f7ff;
            color: #1890ff;
            border: 1px solid #91d5ff;
        }

        .no-order-info {
            text-align: center;
            padding: 40px;
            color: #666;
            background: #f8f9fa;
            border-radius: 12px;
            border: 2px dashed #dee2e6;
        }

        .no-order-info .icon {
            font-size: 3rem;
            margin-bottom: 15px;
            color: #adb5bd;
        }

        .no-order-info h3 {
            margin: 0 0 10px 0;
            color: #495057;
        }

        .no-order-info p {
            margin: 0;
            font-size: 1rem;
        }

        .action-buttons {
            text-align: center;
            margin-top: 30px;
        }

        .action-buttons .btn {
            display: inline-block;
            padding: 12px 24px;
            margin: 0 10px;
            border-radius: 6px;
            text-decoration: none;
            font-size: 1rem;
            font-weight: 500;
            transition: all 0.2s;
            border: none;
            cursor: pointer;
        }

        .btn-primary {
            background: #2450a7;
            color: #fff;
        }

        .btn-primary:hover {
            background: #1a4080;
            transform: translateY(-1px);
        }

        .btn-secondary {
            background: #6c757d;
            color: #fff;
        }

        .btn-secondary:hover {
            background: #5a6268;
            transform: translateY(-1px);
        }

        @media (max-width: 900px) {
            .sidebar {
                width: 120px;
            }

            .menu-item {
                padding: 18px 0 18px 15px;
                text-align: center;
                font-size: 1rem;
            }

            .form-card {
                padding: 20px;
            }

            .main-content {
                padding: 15px;
            }

            .order-details {
                grid-template-columns: 1fr;
            }

            .order-info {
                padding: 20px;
            }

            .search-form {
                flex-direction: column;
                gap: 10px;
            }

            .search-form > div {
                display: flex;
                gap: 10px;
            }
        }
    </style>
</head>

<body>
<div class="header">
    <div>欢迎登录公司用车管理系统（用车员工）</div>
    <div class="header-right">
        <div class="user-icon">👤</div>
        <div class="user-info">账号：<%= employee.getEmployeeID() %></div>
        <div class="bell-icon">🔔</div>
        <a href="${pageContext.request.contextPath}/LogoutServlet" class="logout">退出登录</a>
    </div>
</div>
<div class="container">
    <div class="sidebar">
        <a class="menu-item" href="shouye.jsp">系统首页</a>
        <a class="menu-item" href="shenqing.jsp">申请用车</a>
        <a class="menu-item active" href="${pageContext.request.contextPath}/UseOrderServlet?action=progress">申请进度</a>
        <a class="menu-item" href="${pageContext.request.contextPath}/UseOrderServlet?action=employeeOrders">历史订单</a>
        <a class="menu-item" href="geren.jsp">个人中心</a>
    </div>
    <div class="main-content">
        <div class="form-card">
            <div class="form-title">申请进度查询</div>

            <!-- 显示最新申请订单号 -->
            <% if (latestOrderId != null) { %>
            <div class="latest-order-info">
                <h4><span class="icon">🆔</span>您的最新申请订单号</h4>
                <div class="latest-order-id"><%= latestOrderId %></div>
            </div>
            <% } %>

            <!-- 搜索区域 -->
            <div class="search-section">
                <form class="search-form" method="get" action="${pageContext.request.contextPath}/UseOrderServlet">
                    <input type="hidden" name="action" value="progress">
                    <div class="search-group">
                        <label for="orderId">订单号</label>
                        <input type="text" id="orderId" name="orderId"
                               value="<%= searchOrderId != null ? searchOrderId : "" %>"
                               placeholder="请输入订单号（留空查看最新订单）">
                    </div>
                    <button type="submit" class="search-btn">🔍 查询</button>
                    <button type="button" class="clear-btn" onclick="clearSearch()">清空</button>
                </form>
            </div>

            <!-- 错误消息 -->
            <% if (errorMessage != null) { %>
            <div class="error-message">
                <span class="error-icon">⚠️</span>
                <%= errorMessage %>
            </div>
            <% } %>

            <!-- 显示已处理的订单信息 -->
            <% if (order != null && "processed".equals(orderStatus)) {
                // 获取车辆信息
                Vehicle vehicle = null;
                try {
                    vehicle = vehicleService.getVehicleById(order.getVehicleId());
                } catch (Exception e) {
                    // 处理异常
                }

                // 获取司机信息
                Drivers driver = null;
                try {
                    driver = driversService.getDriverById(order.getDriverId());
                } catch (Exception e) {
                    // 处理异常
                }

                // 动态判断状态样式
                String reviewStatus = order.getReviewStatus();
                String statusClass = "status-default";
                if (reviewStatus != null) {
                    switch (reviewStatus) {
                        case "已通过":
                        case "已批准":
                        case "通过":
                            statusClass = "status-approved";
                            break;
                        case "已拒绝":
                        case "拒绝":
                        case "不通过":
                            statusClass = "status-rejected";
                            break;
                        case "待审核":
                        case "审核中":
                            statusClass = "status-pending";
                            break;
                        case "已完成":
                        case "完成":
                            statusClass = "status-completed";
                            break;
                        default:
                            statusClass = "status-default";
                            break;
                    }
                }
            %>
            <div class="order-info">
                <h3><span class="icon">📋</span>最近完成订单详细信息</h3>

                <div class="order-details">
                    <div class="detail-item">
                        <label>订单号</label>
                        <div class="value"><%= order.getOrderId() %></div>
                    </div>

                    <div class="detail-item">
                        <label>员工账号</label>
                        <div class="value"><%= employee.getEmployeeID() %></div>
                    </div>

                    <div class="detail-item">
                        <label>用车目的</label>
                        <div class="value"><%= order.getUsageReason() != null ? order.getUsageReason() : "未指定" %></div>
                    </div>

                    <div class="detail-item">
                        <label>目的地</label>
                        <div class="value"><%= order.getDestination() != null ? order.getDestination() : "未指定" %></div>
                    </div>

                    <div class="detail-item">
                        <label>出发时间</label>
                        <div class="value"><%= order.getExpectedDepartureTime() != null ? order.getExpectedDepartureTime() : "未设置" %></div>
                    </div>

                    <div class="detail-item">
                        <label>返回时间</label>
                        <div class="value"><%= order.getExpectedReturnTime() != null ? order.getExpectedReturnTime() : "未设置" %></div>
                    </div>

                    <div class="detail-item">
                        <label>车辆信息</label>
                        <div class="value">
                            <%= vehicle != null ? (vehicle.getLicensePlate() + " " + vehicle.getBrandModel()) : "未分配" %>
                        </div>
                    </div>

                    <div class="detail-item">
                        <label>司机信息</label>
                        <div class="value">
                            <%= driver != null ? (driver.getDriverName() + " " + driver.getPhoneNumber()) : "未分配" %>
                        </div>
                    </div>

                    <div class="detail-item">
                        <label>审核状态</label>
                        <div class="value">
                            <span class="status-badge <%= statusClass %>">
                                <%= reviewStatus != null ? reviewStatus : "未知状态" %>
                            </span>
                        </div>
                    </div>

                    <div class="detail-item">
                        <label>备注信息</label>
                        <div class="value">
                            <%= order.getStatusremarks() != null ? order.getStatusremarks() : "无备注信息" %>
                        </div>
                    </div>

                    <div class="detail-item">
                        <label>查询时间</label>
                        <div class="value">
                            <%= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()) %>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 显示审核中的订单信息 -->
            <% } else if (shenqing != null && "reviewing".equals(orderStatus)) { %>
            <div class="order-info">
                <h3><span class="icon">📋</span>订单审核状态</h3>

                <div class="order-details">
                    <div class="detail-item">
                        <label>订单号</label>
                        <div class="value"><%= shenqing.getOrderId() %></div>
                    </div>

                    <div class="detail-item">
                        <label>员工账号</label>
                        <div class="value"><%= employee.getEmployeeID() %></div>
                    </div>

                    <div class="detail-item">
                        <label>车辆类型</label>
                        <div class="value"><%= shenqing.getCarType() != null ? shenqing.getCarType() : "未指定" %></div>
                    </div>

                    <div class="detail-item">
                        <label>用车目的</label>
                        <div class="value"><%= shenqing.getUsageReason() != null ? shenqing.getUsageReason() : "未指定" %></div>
                    </div>

                    <div class="detail-item">
                        <label>目的地</label>
                        <div class="value"><%= shenqing.getDestination() != null ? shenqing.getDestination() : "未指定" %></div>
                    </div>

                    <div class="detail-item">
                        <label>出发时间</label>
                        <div class="value"><%= shenqing.getDepartureTime() != null ? shenqing.getDepartureTime() : "未设置" %></div>
                    </div>

                    <div class="detail-item">
                        <label>返回时间</label>
                        <div class="value"><%= shenqing.getReturnTime() != null ? shenqing.getReturnTime() : "未设置" %></div>
                    </div>

                    <div class="detail-item">
                        <label>审核状态</label>
                        <div class="value">
                            <span class="status-badge status-reviewing">
                                审核中
                            </span>
                        </div>
                    </div>

                    <div class="detail-item">
                        <label>备注信息</label>
                        <div class="value">
                            <%= shenqing.getRemarks() != null ? shenqing.getRemarks() : "无备注信息" %>
                        </div>
                    </div>

                    <div class="detail-item">
                        <label>查询时间</label>
                        <div class="value">
                            <%= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()) %>
                        </div>
                    </div>
                </div>
            </div>
            <% } else if ("none".equals(orderStatus)) { %>
            <div class="no-order-info">
                <div class="icon">📝</div>
                <h3>暂无申请记录</h3>
                <p>您还没有提交过用车申请，请先前往"申请用车"页面提交申请。</p>
            </div>
            <% } %>

            <!-- 操作按钮 -->
            <div class="action-buttons">
                <button class="btn btn-primary" onclick="location.reload()">🔄 刷新状态</button>
                <a href="shenqing.jsp" class="btn btn-secondary">📝 申请用车</a>
                <a href="${pageContext.request.contextPath}/UseOrderServlet?action=employeeOrders" class="btn btn-secondary">📋 查看历史</a>
            </div>
        </div>
    </div>
</div>

<script>
    // 清空搜索
    function clearSearch() {
        document.getElementById('orderId').value = '';
        // 提交表单以显示最新订单
        document.querySelector('.search-form').submit();
    }

    // 页面加载完成后的处理
    window.onload = function() {
        console.log('申请进度页面加载完成');

        // 如果有错误消息，5秒后自动隐藏
        const errorMsg = document.querySelector('.error-message');
        if (errorMsg) {
            setTimeout(() => {
                errorMsg.style.opacity = '0';
                setTimeout(() => {
                    errorMsg.style.display = 'none';
                }, 300);
            }, 5000);
        }
    };

    // 回车键提交搜索
    document.getElementById('orderId').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            document.querySelector('.search-form').submit();
        }
    });
</script>

</body>
</html>
